package com.hunsy.fileupload;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GlobalInterceptor implements Interceptor {

    private Logger logger = LoggerFactory.getLogger(GlobalInterceptor.class);
    @Override
    public void intercept(ActionInvocation ai) {
        //跨域支持
        Controller ctr = ai.getController();
        ctr.getResponse().setHeader("Access-Control-Allow-Origin", "*");
        ctr.getResponse().setHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,Authorization,dtoken");
        //OPTIONS操作
        logger.info("method:{}", ctr.getRequest().getMethod());
        if (ctr.getRequest().getMethod().toUpperCase().equals("OPTIONS")) {
            ctr.renderNull();
        } else {
            ai.invoke();
        }
    }
}
